//
//  BaseNavigationController.m
//  AGG_V2
//
//  Created by MacMini on 15/8/18.
//  Copyright (c) 2015年 admin. All rights reserved.
//

#import "AGGNavigationController.h"
#import "Util.h"


@interface AGGNavigationController () <UINavigationControllerDelegate, UIGestureRecognizerDelegate> {
    UINavigationController *_currentNavigationController;
}

@end

@implementation AGGNavigationController

/**
 *  第一次调用类的时候会调用该方法
 */
+ (void)initialize {
    //设置UINavigationBar的主题
    [self setupNavigationBarTheme];
    [self setupBarButtonItemTheme];
}

/**
 *  设置UINavigationBar的主题
 */
+ (void)setupNavigationBarTheme {
    //通过设置 appearance对象，能够修改整个项目中所有UINavigationBar的样式
    UINavigationBar *appearance = [UINavigationBar appearance];
    [appearance setBarTintColor:RGB(249, 249, 249)];
    [appearance setTitleTextAttributes:[Util getNavigationBarTitleTextAttributes]];
    [appearance setTintColor:RGBJ(44)];
    
    

}

/**
 *  设置UIBarButtonItem的主题
 */
+ (void)setupBarButtonItemTheme {
    //通过设置 appearance对象，能够修改整个项目中所有UIBarButtonItem的样式
    UIBarButtonItem *appearance = [UIBarButtonItem appearance];
    
    //设置文字的属性
    //1.设置普通状态下文字的属性
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    //设置字体
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    
    //设置颜色为橙色
    textAttrs[NSForegroundColorAttributeName] = RGBJ(44);
    [appearance setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    //2.设置高亮状态下文字的属性
    NSMutableDictionary *hightextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
    //设置颜色为红色
    hightextAttrs[NSForegroundColorAttributeName] = RGBJ(44);
    [appearance setTitleTextAttributes:hightextAttrs forState:UIControlStateHighlighted];
    
    //3.设置不可用状态下文字的属性
    NSMutableDictionary *disabletextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
    disabletextAttrs[NSForegroundColorAttributeName] = RGBJ(44);
    [appearance setTitleTextAttributes:disabletextAttrs forState:UIControlStateDisabled];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //手势返回
        self.interactivePopGestureRecognizer.delegate = self;
        self.delegate = self;
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - Override
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        //返回
        self.interactivePopGestureRecognizer.enabled = NO;
    }

    //如果现在push的不是栈顶控制器，则隐藏tabbar工具条
    if (self.viewControllers.count > 0) {
        //隐藏Tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [Util addBarButtonItemWithImage:@"xiangyou.png" highlightImage:nil target:self action:@selector(back)];
    }
    [super pushViewController:viewController animated:animated];
}

#pragma mark - UINavigaionControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    _currentNavigationController = navigationController;
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        if ([navigationController.viewControllers count] == 1) {
            navigationController.interactivePopGestureRecognizer.enabled = NO;
        }else{
            navigationController.interactivePopGestureRecognizer.enabled = YES;
            animated = NO;
        }
    }
}

#pragma mark - back
- (void)back {
    [self popViewControllerAnimated:YES];
}
@end

